<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-02 19:42:06
 * @LastEditTime: 2022-08-25 21:34:16
 * @LastEditors: CHINAKAIYUAN
 * @Description: 
 */

namespace App\Repositories;

use App\Models\Employee;
use App\Models\ProEmployeePro;

/**
 * 产量工资
 * @property ProProcessProRepository $process_pro
 * @property ProEmployeePro $model
 * @author sunkaiyuan
 */
class ProEmployeeProRepository extends Repositories
    {
    protected $relation_models = ['employee', 'shift', 'position'];
    public function boot(ProEmployeePro $model, ProProcessProRepository $process_pro)
        {
        $this->model       = $model;
        $this->process_pro = $process_pro;
        }
    /**
     * 新增产量报工单
     * @param mixed $employee_id 员工 
     * @param mixed $quantity 报工数量
     * @param mixed $billdate 报工日期
     * @return int 返回工序产量ID 如果工序不计产量，返回-1
     * @author sunkaiyuan
     */
    public function add($employee_id, $quantity, $billdate, $route_process_id)
        {
        $billdate       = date("Y-m-d", strtotime($billdate));
        $employee_model = is_numeric($employee_id) ? Employee::whereId($employee_id)->first() : $employee_id;
        $position_model = $employee_model->position; //岗位
        if ($position_model == null) {
            fail("[{$employee_model->name}]该员工没有设置岗位！无法新增产量！");
            }
        $productivity_method_id = $position_model->productivity_method_id; //产量计算方式 1 个人，2班组 -1 不计产量
        if ($productivity_method_id == -1)
            return -1;
        $pro_process_model = $position_model->pro_process; //工序
        $product_rate      = $pro_process_model->product_rate; //产量系数


        $shift_id = $employee_model->shift_id; //班次
        $price    = $employee_model->piece_rate_salary; //计件工价




        $process_pro_model = $this->process_pro->initprocesspro($billdate, $shift_id, $position_model->id, $route_process_id);
        //工序产量
        $process_pro_model->quantity += $quantity;
        //个人产量计件，直接新增
        $salary_amount = 0;
        if ($productivity_method_id == 1) {
            $salary_amount = $this->savepersonpro($billdate, $employee_model->id, $quantity, $product_rate, $shift_id, $position_model->id, $price, $process_pro_model->id, $route_process_id);
            }
        else {
            //班组产量计件，列出 同岗位，同班次的员工，每一个都新增一个产量
            $users = $position_model->users()->whereShift_id($shift_id)->get();
            $users->filter(function ($v) use ($billdate, $quantity, $product_rate, $shift_id, $position_model, &$salary_amount, $process_pro_model, $route_process_id)
                {
                $salary_amount += $this->savepersonpro($billdate, $v->id, $quantity, $product_rate, $shift_id, $position_model->id, $v->piece_rate_salary, $process_pro_model->id, $route_process_id);
                });
            }
        $process_pro_model->amount += $salary_amount;
        $process_pro_model->save();
        return $process_pro_model->id;
        }

    //保存个人产量
    private function savepersonpro($billdate, $employee_id, $quantity, $product_rate, $shift_id, $position_id, $price, $process_pro_id, $route_process_id)
        {

        $pro_salary_model = $this->firstOrCreate(
            [
                'billdate'         => $billdate,
                'employee_id'      => $employee_id,
                'shift_id'         => $shift_id,
                'position_id'      => $position_id,
                'price'            => $price,
                'product_rate'     => $product_rate,
                'route_process_id' => $route_process_id,
            ],
            [
                'process_pro_id' => $process_pro_id,
            ]
        );

        $pro_salary_model->quantity += $quantity;
        $pro_salary_model->amount   = $pro_salary_model->quantity * $pro_salary_model->product_rate * $pro_salary_model->price;
        $pro_salary_model->save();

        return $quantity * $product_rate * $price;
        }
    }
